{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7585270c",
   "metadata": {},
   "source": [
    "# Utility Chains Overview"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "416fc6f5",
   "metadata": {},
   "source": [
    "## Summarizing Documents"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6782af7d",
   "metadata": {},
   "source": [
    "### load_summarize_chain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "db3f812e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk- \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ceb56eaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.llms import OpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains.summarize import load_summarize_chain\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.docstore.document import Document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "333eca01",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(temperature=0.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "369ec2d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reading the document\n",
    "with open(\"sample.txt\") as f:\n",
    "    data = f.read()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75c86c6b",
   "metadata": {},
   "source": [
    "<font color='green'>\n",
    "When it comes to document processing, breaking a large document into smaller, more manageable chunks is essential\n",
    "<font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e378c705",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Split text\n",
    "text_splitter = CharacterTextSplitter()\n",
    "texts = text_splitter.split_text(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1e33b908",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create multiple documents\n",
    "docs = [Document(page_content=t) for t in texts]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "aa7b6158",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(page_content=\"Title: The Computer: Revolutionizing the World of Technology\\n\\nIntroduction:\\nThe computer is a marvel of human ingenuity that has revolutionized the world in countless ways. From its humble beginnings as a complex calculating machine to its current status as a ubiquitous tool in every aspect of our lives, the computer has transformed how we work, communicate, learn, and entertain ourselves. This essay explores the evolution, impact, and future potential of computers in shaping our modern world.\\n\\nThe Birth of the Computer:\\nThe computer has its roots in the early 19th century when inventors and mathematicians began conceptualizing machines capable of automating complex calculations. However, it was not until the mid-20th century that the first electronic digital computers were developed. Pioneers such as Alan Turing, John von Neumann, and Grace Hopper made significant contributions to the field, laying the groundwork for the computers we know today.\\n\\nThe Evolution of Computing Power:\\nFrom room-sized mainframes to portable laptops, computers have evolved exponentially in terms of size, speed, and processing power. The introduction of integrated circuits, microprocessors, and Moore's Law, which states that the number of transistors on a microchip doubles approximately every two years, have propelled the advancement of computer technology. This exponential growth has led to the development of faster, more efficient, and increasingly capable devices that continue to reshape our world.\\n\\nTransforming Work and Productivity:\\nComputers have transformed the workplace, increasing productivity, efficiency, and accuracy across industries. They have automated repetitive tasks, streamlined operations, and facilitated global connectivity. From word processing and data analysis to complex simulations and artificial intelligence, computers have become essential tools for professionals in fields such as finance, healthcare, engineering, and creative arts. The advent of remote work and digital collaboration further underscores the computer's impact on modern work environments.\\n\\nCommunication and Connectivity:\\nThe computer has revolutionized communication, enabling people to connect with one another across vast distances. The internet, a global network of computers, has facilitated instant communication through email, messaging applications, and social media platforms. It has transformed how we share information, access knowledge, and engage in online communities. Additionally, advancements in video conferencing and virtual reality have bridged geographical gaps, allowing for immersive and real-time interactions.\\n\\n\\nEducation and Learning:\\nComputers have had a profound impact on education, revolutionizing the way we learn and acquire knowledge. Online learning platforms, digital textbooks, and educational software provide accessible and interactive learning experiences. Virtual simulations and augmented reality applications enhance understanding in subjects such as science, history, and medicine. Furthermore, computers have expanded access to education, enabling remote learning and distance education opportunities for individuals worldwide.\\n\\nEntertainment and Creativity:\\nComputers have transformed the entertainment industry, enabling the creation and consumption of diverse forms of media. From video games and digital art to music production and film editing, computers have become indispensable tools for creative expression. Streaming services and online platforms have democratized content distribution, offering a plethora of entertainment options to global audiences. Virtual reality and augmented reality technologies offer immersive experiences, blurring the boundaries between the digital and physical realms.\", metadata={}),\n",
       " Document(page_content='Conclusion:\\nThe computer has profoundly shaped our modern world, revolutionizing the way we work, communicate, learn, and entertain ourselves. Its evolution from a bulky calculating machine to a portable device with immense processing power is a testament to human innovation. As computers continue to advance, their potential to drive societal progress, solve complex problems, and inspire new avenues of creativity is limitless. With responsible development and ethical usage, the computer will undoubtedly remain at the forefront of technological advancements, empowering individuals and transforming societies for generations to come.', metadata={})]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "docs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bf24b6b",
   "metadata": {},
   "source": [
    "<font color='green'>\n",
    "To create an instance of load_summarizer_chain, we need to provide three arguments. <br><br>Firstly, we need to pass the desired large language model that will be used to query the user input. Secondly, we specify the type of langchain chain to be used for summarizing documents.<br> Lastly, we can set the verbose argument to True if we want to see all the intermediate steps involved in processing the user request and generating the output.<font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a582191f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001B[1m> Entering new MapReduceDocumentsChain chain...\u001B[0m\n",
      "\n",
      "\n",
      "\u001B[1m> Entering new LLMChain chain...\u001B[0m\n",
      "Prompt after formatting:\n",
      "\u001B[32;1m\u001B[1;3mWrite a concise summary of the following:\n",
      "\n",
      "\n",
      "\"Title: The Computer: Revolutionizing the World of Technology\n",
      "\n",
      "Introduction:\n",
      "The computer is a marvel of human ingenuity that has revolutionized the world in countless ways. From its humble beginnings as a complex calculating machine to its current status as a ubiquitous tool in every aspect of our lives, the computer has transformed how we work, communicate, learn, and entertain ourselves. This essay explores the evolution, impact, and future potential of computers in shaping our modern world.\n",
      "\n",
      "The Birth of the Computer:\n",
      "The computer has its roots in the early 19th century when inventors and mathematicians began conceptualizing machines capable of automating complex calculations. However, it was not until the mid-20th century that the first electronic digital computers were developed. Pioneers such as Alan Turing, John von Neumann, and Grace Hopper made significant contributions to the field, laying the groundwork for the computers we know today.\n",
      "\n",
      "The Evolution of Computing Power:\n",
      "From room-sized mainframes to portable laptops, computers have evolved exponentially in terms of size, speed, and processing power. The introduction of integrated circuits, microprocessors, and Moore's Law, which states that the number of transistors on a microchip doubles approximately every two years, have propelled the advancement of computer technology. This exponential growth has led to the development of faster, more efficient, and increasingly capable devices that continue to reshape our world.\n",
      "\n",
      "Transforming Work and Productivity:\n",
      "Computers have transformed the workplace, increasing productivity, efficiency, and accuracy across industries. They have automated repetitive tasks, streamlined operations, and facilitated global connectivity. From word processing and data analysis to complex simulations and artificial intelligence, computers have become essential tools for professionals in fields such as finance, healthcare, engineering, and creative arts. The advent of remote work and digital collaboration further underscores the computer's impact on modern work environments.\n",
      "\n",
      "Communication and Connectivity:\n",
      "The computer has revolutionized communication, enabling people to connect with one another across vast distances. The internet, a global network of computers, has facilitated instant communication through email, messaging applications, and social media platforms. It has transformed how we share information, access knowledge, and engage in online communities. Additionally, advancements in video conferencing and virtual reality have bridged geographical gaps, allowing for immersive and real-time interactions.\n",
      "\n",
      "\n",
      "Education and Learning:\n",
      "Computers have had a profound impact on education, revolutionizing the way we learn and acquire knowledge. Online learning platforms, digital textbooks, and educational software provide accessible and interactive learning experiences. Virtual simulations and augmented reality applications enhance understanding in subjects such as science, history, and medicine. Furthermore, computers have expanded access to education, enabling remote learning and distance education opportunities for individuals worldwide.\n",
      "\n",
      "Entertainment and Creativity:\n",
      "Computers have transformed the entertainment industry, enabling the creation and consumption of diverse forms of media. From video games and digital art to music production and film editing, computers have become indispensable tools for creative expression. Streaming services and online platforms have democratized content distribution, offering a plethora of entertainment options to global audiences. Virtual reality and augmented reality technologies offer immersive experiences, blurring the boundaries between the digital and physical realms.\"\n",
      "\n",
      "\n",
      "CONCISE SUMMARY:\u001B[0m\n",
      "Prompt after formatting:\n",
      "\u001B[32;1m\u001B[1;3mWrite a concise summary of the following:\n",
      "\n",
      "\n",
      "\"Conclusion:\n",
      "The computer has profoundly shaped our modern world, revolutionizing the way we work, communicate, learn, and entertain ourselves. Its evolution from a bulky calculating machine to a portable device with immense processing power is a testament to human innovation. As computers continue to advance, their potential to drive societal progress, solve complex problems, and inspire new avenues of creativity is limitless. With responsible development and ethical usage, the computer will undoubtedly remain at the forefront of technological advancements, empowering individuals and transforming societies for generations to come.\"\n",
      "\n",
      "\n",
      "CONCISE SUMMARY:\u001B[0m\n",
      "\n",
      "\u001B[1m> Finished chain.\u001B[0m\n",
      "\n",
      "\n",
      "\u001B[1m> Entering new StuffDocumentsChain chain...\u001B[0m\n",
      "\n",
      "\n",
      "\u001B[1m> Entering new LLMChain chain...\u001B[0m\n",
      "Prompt after formatting:\n",
      "\u001B[32;1m\u001B[1;3mWrite a concise summary of the following:\n",
      "\n",
      "\n",
      "\"\n",
      "The computer is an incredible invention that has revolutionized the world in countless ways. This essay explores the evolution, impact, and future potential of computers, from the first electronic digital computers to the current integrated circuits and microprocessors. Computers have transformed the workplace, increased productivity, facilitated global connectivity, revolutionized communication, transformed education and learning, and enabled the creation and consumption of diverse forms of media. With continued developments in technology, computers will continue to shape and reshape our world.\n",
      "\n",
      " Computers have drastically altered our lives, evolving from calculators to powerful devices. As computers continue to develop, their potential for societal improvement, problem solving, and creativity will be limitless. Responsible development and ethical use of computers can enable individuals and societies to benefit from their transformative power for generations to come.\"\n",
      "\n",
      "\n",
      "CONCISE SUMMARY:\u001B[0m\n",
      "\n",
      "\u001B[1m> Finished chain.\u001B[0m\n",
      "\n",
      "\u001B[1m> Finished chain.\u001B[0m\n",
      "\n",
      "\u001B[1m> Finished chain.\u001B[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' This essay examines the development of computers from the first electronic digital computers to modern integrated circuits and microprocessors and the ways in which they have changed the world. Computers have revolutionized the workplace, increased productivity, connected the world, advanced communication, and transformed education and media. With further technological advancements, computers will continue to shape our world. Responsible development and usage of computers could benefit individuals and societies for generations.'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain = load_summarize_chain(llm, chain_type=\"map_reduce\", verbose=True)\n",
    "chain.run(docs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3593072c",
   "metadata": {},
   "source": [
    "## HTTP Requests"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "320a57a5",
   "metadata": {},
   "source": [
    "### LLMRequestsChain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2a29e1b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import LLMRequestsChain, LLMChain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e1f23480",
   "metadata": {},
   "outputs": [],
   "source": [
    "template = \"\"\"\n",
    "Extract the answer to the question '{query}' or say \"not found\" if the information is not available.\n",
    "{requests_result}\n",
    "\"\"\"\n",
    "\n",
    "PROMPT = PromptTemplate(\n",
    "    input_variables=[\"query\", \"requests_result\"],\n",
    "    template=template,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a5dc9937",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm=OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "dd539d4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = LLMRequestsChain(llm_chain=LLMChain(llm=llm, prompt=PROMPT))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb3fbc27",
   "metadata": {},
   "source": [
    "<font color='green'>\n",
    "Preparing the question & inputs to the http request<font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "64893b39",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"What is the capital of india?\"\n",
    "inputs = {\n",
    "    \"query\": question,\n",
    "    \"url\": \"https://www.google.com/search?q=\" + question.replace(\" \", \"+\"),\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "779f0411",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIConnectionError: Error communicating with OpenAI: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')).\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'query': 'What is the capital of india?',\n",
       " 'url': 'https://www.google.com/search?q=What+is+the+capital+of+india?',\n",
       " 'output': '\\nNew Delhi'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain(inputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14c980e2",
   "metadata": {},
   "source": [
    "<font color='green'>\n",
    "Let's look at the internal functioning<font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b974d187",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    def _call(\n",
      "        self,\n",
      "        inputs: Dict[str, Any],\n",
      "        run_manager: Optional[CallbackManagerForChainRun] = None,\n",
      "    ) -> Dict[str, Any]:\n",
      "        from bs4 import BeautifulSoup\n",
      "\n",
      "        _run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager()\n",
      "        # Other keys are assumed to be needed for LLM prediction\n",
      "        other_keys = {k: v for k, v in inputs.items() if k != self.input_key}\n",
      "        url = inputs[self.input_key]\n",
      "        res = self.requests_wrapper.get(url)\n",
      "        # extract the text from the html\n",
      "        soup = BeautifulSoup(res, \"html.parser\")\n",
      "        other_keys[self.requests_key] = soup.get_text()[: self.text_length]\n",
      "        result = self.llm_chain.predict(\n",
      "            callbacks=_run_manager.get_child(), **other_keys\n",
      "        )\n",
      "        return {self.output_key: result}\n"
     ]
    }
   ],
   "source": [
    "import inspect\n",
    "print(inspect.getsource(chain._call))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
